import requests
import plotly.express as px 
url = "https://api.github.com/search/repositories"
url += "?q=language:China+sort:stars+stars:>10000"

headers = {"Accept":"application/vnd.github.v3+json"}
r = requests.get(url,headers=headers)
print(f"Status code:{r.status_code}")

response_dict = r.json()
print(f"总共的仓库数:{response_dict["total_count"]}")
print(f"incomplete_results:{response_dict["incomplete_results"]}")

repo = response_dict["items"]
print(f"items:{len(repo)}")
repo_links, stars,hover_texts = [],[],[]
for repo_dict in repo:
    repo_url = repo_dict["html_url"]
    repo_name = repo_dict['name']
    repo_link = f"<a href= '{repo_url}'>{repo_name}</a>"
    repo_links.append(repo_link)     
    stars.append(repo_dict["stargazers_count"])
    owner = repo_dict['owner']["login"]
    description = repo_dict["description"]
    hover_text = f"{owner}<br />{description}"
    hover_texts.append(hover_text)
    # print("\n")
    # print(f"Name: {repo_dict["name"]}")
    # print(f"Owner: {repo_dict["owner"]["login"]}")
    # print(f"Stars: {repo_dict["stargazers_count"]}")
    
title = "Mast-Starred Python Projects on Github"
labels = {'x':'Repository','y':'Stars'}
fig = px.bar(x=repo_links, y = stars,hover_name=hover_texts,title=title,labels=labels)
fig.update_layout(title_font_size = 28, xaxis_title_font_size = 20,yaxis_title_font_size = 20)
fig.update_traces(marker_color = 'SteelBlue',marker_opacity = 0.6)
fig.show()
# repo_dict = repo[0]
# print(f"\nKeys:{len(repo_dict)}")
# for key in sorted(repo_dict.keys()):
#     print(key)



